package com.competition.commgr.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.competition.common.utils.DateUtils;
import com.competition.common.utils.SecurityUtils;
import com.competition.commgr.constants.ReviewStepConstants; // 新增导入
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.competition.common.annotation.Log;
import com.competition.common.core.controller.BaseController;
import com.competition.common.core.domain.AjaxResult;
import com.competition.common.enums.BusinessType;
import com.competition.commgr.domain.RegistrationReview;
import com.competition.commgr.service.IRegistrationReviewService;
import com.competition.common.utils.poi.ExcelUtil;
import com.competition.common.core.page.TableDataInfo;

/**
 * 报名审核Controller
 */
@RestController
@RequestMapping("/commgr/review")
public class RegistrationReviewController extends BaseController
{
    @Autowired
    private IRegistrationReviewService registrationReviewService;

    /**
     * 查询报名审核列表
     */
    @PreAuthorize("@ss.hasPermi('commgr:review:list')")
    @GetMapping("/list")
    public TableDataInfo list(RegistrationReview registrationReview)
    {
        startPage();
        List<RegistrationReview> list = registrationReviewService.selectRegistrationReviewList(registrationReview);
        return getDataTable(list);
    }

    /**
     * 导出报名审核列表
     */
    @PreAuthorize("@ss.hasPermi('commgr:review:export')")
    @Log(title = "报名审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RegistrationReview registrationReview)
    {
        List<RegistrationReview> list = registrationReviewService.selectRegistrationReviewList(registrationReview);
        ExcelUtil<RegistrationReview> util = new ExcelUtil<RegistrationReview>(RegistrationReview.class);
        util.exportExcel(response, list, "报名审核数据");
    }

    /**
     * 获取报名审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('commgr:review:query')")
    @GetMapping(value = "/{reviewId}")
    public AjaxResult getInfo(@PathVariable("reviewId") Long reviewId)
    {
        RegistrationReview review = registrationReviewService.selectRegistrationReviewByReviewId(reviewId);
        // 设置显示名称（如果需要）
        if (review != null && review.getStep() != null) {
            review.setStep(ReviewStepConstants.getDisplayName(review.getStep()));
        }
        return success(review);
    }

    /**
     * 新增报名审核
     */
    @PreAuthorize("@ss.hasPermi('commgr:review:add')")
    @Log(title = "报名审核", businessType = BusinessType.INSERT)
    // 在Controller的add方法中，移除步骤规范化，因为Service层已经处理了
    @PostMapping
    public AjaxResult add(@RequestBody RegistrationReview registrationReview)
    {
        // Service层已经处理步骤规范化，这里只需要业务逻辑
        if(!ReviewStepConstants.isTrainingCenterStep(registrationReview.getStep())){
            // 先更新当前阶段的审批状态
            registrationReviewService.updateRegistrationReview(registrationReview);
        }

        // 设置提交时间
        registrationReview.setCommitTime(DateUtils.getNowDate());

        return toAjax(registrationReviewService.insertRegistrationReview(registrationReview));
    }

    /**
     * 修改报名审核
     */
    @Log(title = "报名审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RegistrationReview registrationReview)
    {
        // Service层已经处理步骤规范化
        registrationReview.setCommitTime(DateUtils.getNowDate());
        return toAjax(registrationReviewService.updateRegistrationReview(registrationReview));
    }

    /**
     * 删除报名审核
     */
    @PreAuthorize("@ss.hasPermi('commgr:review:remove')")
    @Log(title = "报名审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reviewIds}")
    public AjaxResult remove(@PathVariable Long[] reviewIds)
    {
        return toAjax(registrationReviewService.deleteRegistrationReviewByReviewIds(reviewIds));
    }

    /**
     * 获取审核步骤选项（用于前端下拉框）
     */
    @GetMapping("/stepOptions")
    public AjaxResult getStepOptions() {
        List<Map<String, String>> options = new ArrayList<>();

        Map<String, String> college = new HashMap<>();
        college.put("value", ReviewStepConstants.COLLEGE);
        college.put("label", ReviewStepConstants.COLLEGE_NAME);
        options.add(college);

        Map<String, String> trainingCenter = new HashMap<>();
        trainingCenter.put("value", ReviewStepConstants.TRAINING_CENTER);
        trainingCenter.put("label", ReviewStepConstants.TRAINING_CENTER_NAME);
        options.add(trainingCenter);

        return AjaxResult.success(options);
    }
}